home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / news / inn1.000 / inn1.4sec-linux-src.tar / inn / lib / clientlib.c < prev    next >
C/C++ Source or Header  |  1993-03-18  |  3KB  |  165 lines

  1. /*  $Revision: 1.6 $
  2. **
  3. **  Routines compatible with the NNTP "clientlib" routines.
  4. */
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include "configdata.h"
  8. #include "nntp.h"
  9. #include "paths.h"
  10. #include "libinn.h"
  11. #include "clibrary.h"
  12. #include "myserver.h"
  13.  
  14.  
  15. FILE    *ser_rd_fp = NULL;
  16. FILE    *ser_wr_fp = NULL;
  17. char    ser_line[NNTP_STRLEN];
  18.  
  19.  
  20. /*
  21. **  Get the name of the NNTP server.  Ignore the filename; we use
  22. **  our own configuration stuff.  Return pointer to static data.
  23. */
  24. /* ARGSUSED0 */
  25. char *
  26. getserverbyfile(file)
  27.     char    *file;
  28. {
  29.     static char    buff[256];
  30.     char    *p;
  31.  
  32.     if ((p = GetConfigValue(_CONF_SERVER)) == NULL)
  33.     return NULL;
  34.     (void)strcpy(buff, p);
  35.     return buff;
  36. }
  37.  
  38.  
  39. /*
  40. **  Get a connection to the remote news server.  Return server's reply
  41. **  code or -1 on error.
  42. */
  43. int
  44. server_init(host)
  45.     char    *host;
  46. {
  47.     char    line2[NNTP_STRLEN];
  48.  
  49.     if (NNTPconnect(host, &ser_rd_fp, &ser_wr_fp, ser_line) < 0) {
  50.     if (ser_line[0] == '\0')
  51.         /* I/O problem. */
  52.         return -1;
  53.  
  54.     /* Server rejected connection; return it's reply code. */
  55.     return atoi(ser_line);
  56.     }
  57.  
  58.     /* Send the INN command; if understood, use that reply. */
  59.     put_server("mode reader");
  60.     if (get_server(line2, (int)sizeof line2) < 0)
  61.     return -1;
  62.     if (atoi(line2) != NNTP_BAD_COMMAND_VAL)
  63.     (void)strcpy(ser_line, line2);
  64.  
  65.     /* Connected; return server's reply code. */
  66.     return atoi(ser_line);
  67. }
  68.  
  69.  
  70. #define CANTPOST    \
  71.     "NOTE:  This machine does not have permission to post articles"
  72. #define CANTUSE        \
  73.     "This machine does not have permission to use the %s news server.\n"
  74. /*
  75. **  Print a message based on the the server's initial response.
  76. **  Return -1 if server wants us to go away.
  77. */
  78. int
  79. handle_server_response(response, host)
  80.     int        response;
  81.     char    *host;
  82. {
  83.     char    *p;
  84.  
  85.     switch (response) {
  86.     default:
  87.     (void)printf("Unknown response code %d from %s.\n", response, host);
  88.     return -1;
  89.      case NNTP_GOODBYE_VAL:
  90.     if (atoi(ser_line) == response) {
  91.         p = &ser_line[strlen(ser_line) - 1];
  92.         if (*p == '\n' && *--p == '\r')
  93.         *p = '\0';
  94.         if (p > &ser_line[3]) {
  95.         (void)printf("News server %s unavailable: %s\n", host,
  96.             &ser_line[4]);
  97.         return -1;
  98.         }
  99.     }
  100.     (void)printf("News server %s unavailable, try later.\n", host);
  101.     return -1;
  102.     case NNTP_ACCESS_VAL:
  103.     (void)printf(CANTUSE, host);
  104.     return -1;
  105.     case NNTP_NOPOSTOK_VAL:
  106.     (void)printf("%s.\n", CANTPOST);
  107.     /* FALLTHROUGH */
  108.     case NNTP_POSTOK_VAL:
  109.     break;
  110.     }
  111.     return 0;
  112. }
  113.  
  114.  
  115. /*
  116. **  Send a line of text to the server.
  117. */
  118. void
  119. put_server(buff)
  120.     char    *buff;
  121. {
  122.     (void)fprintf(ser_wr_fp, "%s\r\n", buff);
  123.     (void)fflush(ser_wr_fp);
  124. }
  125.  
  126.  
  127. /*
  128. **  Get a line of text from the server, strip trailing \r\n.
  129. **  Return -1 on error.
  130. */
  131. int
  132. get_server(buff, buffsize)
  133.     register char    *buff;
  134.     int            buffsize;
  135. {
  136.     register char    *p;
  137.  
  138.     if (fgets(buff, buffsize, ser_rd_fp) == NULL)
  139.     return -1;
  140.     p = &buff[strlen(buff)];
  141.     if (p >= &buff[2] && p[-2] == '\r' && p[-1] == '\n')
  142.     p[-2] = '\0';
  143.     return 0;
  144. }
  145.  
  146.  
  147. /*
  148. **  Send QUIT and close the server.
  149. */
  150. void
  151. close_server()
  152. {
  153.     char    buff[NNTP_STRLEN];
  154.  
  155.     if (ser_wr_fp != NULL && ser_rd_fp != NULL) {
  156.     put_server("QUIT");
  157.     (void)fclose(ser_wr_fp);
  158.     ser_wr_fp = NULL;
  159.  
  160.     (void)get_server(buff, (int)sizeof buff);
  161.     (void)fclose(ser_rd_fp);
  162.     ser_rd_fp = NULL;
  163.     }
  164. }
  165.